using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BonusesInChestWindow : MonoBehaviour
{
	public static BonusesInChestWindow instance;

	public GameObject centerPanel;

	public GameObject leftPanel;

	public GameObject butBuyChest;

	public GameObject butBuyChestDisabled;

	public UIGrid gridScript;

	public UILabel lbDollarPrice;

	public UILabel lbDollarPriceDisabled;

	public UILabel lbAddMoney;

	public UILabel lbTypePrice;

	public UILabel lbX3AddMoney;

	public UITexture iconProduct;

	public BonuseInChestButton exampleBut;

	private List<BonuseInChestButton> listButton = new List<BonuseInChestButton>();

	private void Awake()
	{
		instance = this;
	}

	private void Start()
	{
		HideWindow();
	}

	private void OnEnable()
	{
		StartCoroutine(Reset());
	}

	private IEnumerator Reset()
	{
		yield return null;
		gridScript.GetComponentInParent<UIScrollView>().ResetPosition();
	}

	private void OnDestroy()
	{
		instance = null;
	}

	public void ShowWindowForChest(ChestBonusItem curChest, InAppProduct curProdCoins, Texture icon)
	{
		if (curChest != null)
		{
			CreateListChestBonuses(curChest);
		}
		iconProduct.mainTexture = icon;
		if (curProdCoins != null)
		{
			if (curProdCoins.but.activeSelf)
			{
				butBuyChest.SetActive(true);
				butBuyChestDisabled.SetActive(false);
			}
			else
			{
				butBuyChest.SetActive(false);
				butBuyChestDisabled.SetActive(true);
			}
			lbAddMoney.text = curProdCoins.lbGiveCoins.text;
			UILabel[] componentsInChildren = lbAddMoney.GetComponentsInChildren<UILabel>(true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				componentsInChildren[i].text = lbAddMoney.text;
			}
			if (curProdCoins.x3Sprite.activeSelf && !string.IsNullOrEmpty(lbAddMoney.text))
			{
				lbX3AddMoney.gameObject.SetActive(true);
				lbX3AddMoney.text = curProdCoins.x3price.text;
			}
			else
			{
				lbX3AddMoney.gameObject.SetActive(false);
			}
			if (curProdCoins.lbTypeCash != null)
			{
				lbTypePrice.text = curProdCoins.lbTypeCash.text;
			}
			lbDollarPrice.text = curProdCoins.dollarPrice + " $";
			lbDollarPriceDisabled.text = lbDollarPrice.text;
		}
		else
		{
			butBuyChest.SetActive(false);
			butBuyChestDisabled.SetActive(false);
			lbAddMoney.text = string.Empty;
		}
		centerPanel.SetActive(true);
		leftPanel.SetActive(true);
		base.gameObject.SetActive(true);
	}

	public void HideWindow()
	{
		centerPanel.SetActive(false);
		leftPanel.SetActive(false);
		base.gameObject.SetActive(false);
		RemoveAllButton();
	}

	private void RemoveAllButton()
	{
		foreach (BonuseInChestButton item in listButton)
		{
			Object.Destroy(item.gameObject);
		}
		listButton.Clear();
	}

	private void CreateListChestBonuses(ChestBonusItem curChest)
	{
		if (curChest != null)
		{
			ChestTir chestTirForCurrentLevel = curChest.GetChestTirForCurrentLevel();
			if (chestTirForCurrentLevel != null)
			{
				SetButtonCount(chestTirForCurrentLevel.items.Count);
				for (int i = 0; i < listButton.Count && i < chestTirForCurrentLevel.items.Count; i++)
				{
					BonuseInChestButton bonuseInChestButton = listButton[i];
					bonuseInChestButton.gameObject.name = i.ToString();
					listButton[i].SetData(chestTirForCurrentLevel.items[i]);
				}
			}
		}
		Sort();
		Invoke("Sort", 0.2f);
	}

	private void Sort()
	{
		if (gridScript != null)
		{
			gridScript.repositionNow = true;
		}
	}

	private void SetButtonCount(int needCount)
	{
		if (listButton.Count < needCount)
		{
			for (int i = listButton.Count; i < needCount; i++)
			{
				BonuseInChestButton item = CreateButton();
				listButton.Add(item);
			}
		}
		else if (listButton.Count > needCount)
		{
			int num = listButton.Count - needCount;
			for (int j = 0; j < num; j++)
			{
				GameObject obj = listButton[listButton.Count - 1].gameObject;
				listButton[listButton.Count - 1] = null;
				listButton.RemoveAt(listButton.Count - 1);
				Object.Destroy(obj);
			}
		}
	}

	private BonuseInChestButton CreateButton()
	{
		GameObject gameObject = Object.Instantiate(exampleBut.gameObject, Vector3.zero, Quaternion.identity) as GameObject;
		BonuseInChestButton component = gameObject.GetComponent<BonuseInChestButton>();
		gameObject.transform.parent = gridScript.transform;
		gameObject.SetActive(true);
		gameObject.transform.localScale = new Vector3(1f, 1f, 1f);
		return component;
	}

	public void BuyChooseProduct()
	{
		if (ChestBonusController.instance != null && ChestBonusController.instance.chooseProdCoins != null)
		{
			InAppShop.instance.buyProductCoinsWithID(ChestBonusController.instance.chooseProdCoins.idItems);
		}
	}
}
